package http

import (
	"mime"
	"net/http"
	"path"
	path2 "xrt/utils/file/path"
)

func SetContentTypeFromExtension(w http.ResponseWriter, extension string) {
	mt := mime.TypeByExtension(extension)
	if mt != "" {
		w.Header().Set("Content-Type", mt)
	}
}

func DisposeStaticResource(x *XHttp) {
	targetFile := x.Pattern[1]
	for _, resourcePath := range x.sync.staticPath {
		targetPath := path.Join(resourcePath, targetFile)
		if fileExists, _ := path2.Exists(targetPath); fileExists {
			SetContentTypeFromExtension(x.Writer, targetPath)
			x.WriteFile(targetPath)
			return
		}
	}
	x.RedirectTo(ERR404, true)
}

func init() {
	mime.AddExtensionType(".css", "text/css")
}
